import { createVNode, render } from "vue";
import tr from "./tr.vue";
import szdStoreSession from "../../common/store/storeSession";

let ctx;

/**
 * 传输请求弹窗
 *
 * @return Object 以下是对象说明 {trCode:请求号,operRemark:请求摘要}
 */
export function szdTr() {
  return new Promise(resolve => {
    const trCtrl = szdStoreSession.getWebParams("tr_ctrl");
    if (trCtrl && trCtrl === 'off') {
      let resTr = {};
      resolve(resTr);
    } else {
      let resolveFlag = false;
      const div = document.createElement("div");
      const vNode = createVNode(tr, {
        onOkay: resTr => {
          resolve(resTr);
          resolveFlag = true;
        },
        onCloseForm: () => {
          if (!resolveFlag) {
            resolve(false);
          }
          document.body.removeChild(div);
        }
      });
      vNode.appContext = ctx;
      document.body.appendChild(div);
      render(vNode, div);
    }
  });
}

/**
 * 设置上下文
 */
const setTr = function (app) {
  ctx = app._context;
};

export default setTr;
